package com.redroid.findme;

/*
 * Connect.java
 * Purpose: activity que dá inicio ao processo de conexão com um outro
 * dispositivo.
 * 
 * @author: Thassio Carvalho <tvmcarvalho@gmail.com>
 * 
 */

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.Log;
import android.util.SparseBooleanArray;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

public class Connect extends Activity implements Parcelable{

	/*
	 *Deve ser implementada uma classe de busca de dispositivos bluetooth
	 *e a mesma instanciada nessa classe. 
	 */
	
	DbHandler db = new DbHandler(this);
	List<Contact> clist;
	List<Contact> sending_cont_list;
	Button btnNext, btnBack, btnCancel;
	
	@Override
	protected void onCreate(Bundle connstate) {
		super.onCreate(connstate);
		setContentView(R.layout.connect_sn_act);
		
		clist = db.getAllContacts();
		ArrayList<String> users = new ArrayList<String>();
		
		for (Contact contact : clist) {
			String useraccount = contact.getUsername() + " (" + 
					contact.getSN() + ")";
				users.add(useraccount);	}
		
		final ListView connect_list = (ListView)findViewById(R.id.connect_sn_act_listview);
		connect_list.setAdapter(new ArrayAdapter<String>(this, 
				android.R.layout.simple_list_item_checked, users));
		connect_list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

		btnNext.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				
				SparseBooleanArray sba = connect_list.getCheckedItemPositions();
				Contact cnt;
				String  sendlisttag = "Send List"; 
				Log.d(sendlisttag, "Building contact list to send");
				
				for (int i = 0; i < sba.size(); i++) {
					if (sba.valueAt(i)==true) {
					cnt = (Contact) connect_list.getItemAtPosition(i);
					sending_cont_list.add(cnt);	
					String curContact = "Added: " + sending_cont_list.get(i).
					getUsername() + " (" + sending_cont_list.get(i).getSN() + ")";
					Log.d(sendlisttag, curContact);
					}
					
					Intent choosedevice = new Intent(Connect.this, ChsDvcAct.class);
					Bundle bnd = new Bundle();
					bnd.putParcelable("sndlcntlist", (Parcelable) sending_cont_list);
					choosedevice.putExtras(bnd);
					startActivity(choosedevice);
				}
				
			}
		});
		
		btnBack.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				onPause();
			}
		});
		
		btnCancel.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
	}
		
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		clist = db.getAllContacts();
		ArrayList<String> users = new ArrayList<String>();
		
		for (Contact contact : clist) {
			String useraccount = contact.getUsername() + " (" + 
					contact.getSN() + ")";
				users.add(useraccount);
		}
		
		ListView connect_list = (ListView)findViewById(R.id.connect_sn_act_listview);
		connect_list.setAdapter(new ArrayAdapter<String>(this, 
				android.R.layout.simple_list_item_checked, users));
		connect_list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
	}
	
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}


	public int describeContents() {
		// TODO Auto-generated method stub
		return 0;
	}


	public void writeToParcel(Parcel dest, int flags) {
		// TODO Auto-generated method stub
		
	}
	
}
